package  {
	import classes.buttons.BaseButton;
	import classes.controlers.XML_Controler;
	import classes.global.Global_Properties;
	import classes.managers.CoinView_Manager;
	import classes.managers.Game_Manager;
	import classes.managers.GameOver_Manager;
	import classes.managers.IntroAni_Manager;
	import classes.managers.Rank_Manager;
	import classes.managers.ViewStateManager;
	import classes.sounds.SoundCollection;
	import flash.display.MovieClip;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.media.SoundMixer;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author Kihoonee 줄넘기.
	 */
	public class Main extends MovieClip {
		
		/*
			Robby와 연동하는 변수.
		*/
		private static var _main:Main;
		private var _lobby:Object;
		
		
		private var sound:AllSound_Mc = new AllSound_Mc();
		
		private var closeBtn:BaseButton;
		
		private var viewManager:ViewStateManager;
		private var introAni:IntroAni_Manager;
		private var game:Game_Manager;
		private var gameOver:GameOver_Manager;
		private var coin:CoinView_Manager;
		public var rank:Rank_Manager;
		private var xmlControler:XML_Controler;
		
		public function Main() {
			if ( stage ) init();
			else addEventListener(Event.ADDED_TO_STAGE, ADDED_TO_STAGE );
		}
		
		private function ADDED_TO_STAGE(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, ADDED_TO_STAGE );
			
			
			var timer:Timer = new Timer(50, 1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent):void {
				timer.removeEventListener(TimerEvent.TIMER_COMPLETE, arguments.callee);
				init();
			});
			
			timer.start();
		}
		
		private function init():void {
			addEvent();
			
			SoundCollection.sound = sound;
			
			
			//Global_Properties.nick = _lobby.nick;
			//Global_Properties.keyCode = _lobby.userKey;
			
			
			viewManager = new ViewStateManager( this );
			introAni = new IntroAni_Manager( this, this.introAni_mc );
			game =  new Game_Manager( this, this.game_mc );
			gameOver = new GameOver_Manager( this, this.gameOver_mc );
			coin =  new CoinView_Manager( this, this.coin_mc );
			rank = new Rank_Manager( this, this.rank_mc );
			xmlControler = new XML_Controler( this );
			
			introAni.init();
			
			SoundCollection.introBgmSound();
			viewChange( "introAni" );
			//SoundCollection.bgmSound();
		}
		
		public function viewChange( _name:String ):void {
			viewManager.viewChange( _name );
		}
		
		public function gameInit():void {
			closeBtn.x = 975;
			closeBtn.y = 20;
			SoundMixer.stopAll();
			SoundCollection.bgmSound();
			game.init();
			
		}
		
		public function gameOverInit():void {
			SoundCollection.failSound();
			gameOver.init();
		}
		
		public function coinInit():void {
			SoundCollection.rewardSound();
			coin.init();
		}
		
		public function rankInit():void {
			//Global_Properties.nick = _lobby.nick;
			//Global_Properties.keyCode = _lobby.userKey;
			xmlControler.load();
		}
		
		public function retry():void {
			//introAni.init();
			//viewManager.viewChange( "introAni" );
			
			gameInit();
			viewChange( "game" );
		}
		
		public function end():void {
			trace( "end" );
			SoundMixer.stopAll();
			exit();
		}
		
		private function addEvent():void {
			closeBtn = new BaseButton( this.close_btn );
			closeBtn.addEventListener( MouseEvent.CLICK, BtnEvent );
		}
		
		private function BtnEvent(e:MouseEvent):void {
			end();
		}
		
		
		//--------------------- 메인로비와 연동 -----------------------------
		
		public function deposit(coin:uint):void { // ----------------	5. 코인 누적 메소드
			if (_lobby) {
				_lobby.deposit(coin);
			}
		}
		public function exit():void { // -----------------------------	6. 게임 종료 메소드
			if (_lobby) {
				_lobby.exit();
			}
		}
		
		public function set lobby(value:Object):void { 
			_lobby = value;   // -----------	3. _lobby setter;
			Global_Properties.nick = _lobby.nick;
			Global_Properties.keyCode = _lobby.userKey;
			trace("userKey : ",Global_Properties.keyCode)
			trace("nick : ",Global_Properties.nick)
		} 
		public static function get main():Main { 
			return _main;    // ---	4. _main getter;
		} 
	}

}